package com.team.hiring.talent.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.team.hiring.common.util.DBUtil;
import com.team.hiring.talent.entity.TalentEntity;

public class TalentDao {

	/**
	 * 求职者登录
	 * 
	 * @param code
	 * @param password
	 */
	public    TalentEntity  login(String code, String password) {
        
		TalentEntity talentEntity=new  TalentEntity();
		try {
			//1、加载驱动
			//Class.forName("com.mysql.jdbc.Driver");
			//2、连接数据库
			//Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hiring", "root", "root");
			Connection connection = DBUtil.getConnection();
			//3、操作数据库
			String sql="select * from talent where code=? and password=?";
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, code);
			preparedStatement.setString(2, password);
			ResultSet resultSet = preparedStatement.executeQuery();
			if(resultSet.next()){
			         talentEntity.setId(resultSet.getInt("id"));
			         talentEntity.setCode(resultSet.getString("code"));
			         talentEntity.setName(resultSet.getString("name"));
			         talentEntity.setAge(resultSet.getInt("age"));
			         talentEntity.setPassword(resultSet.getString("password"));
			         talentEntity.setEmail(resultSet.getString("email"));
			         talentEntity.setHobby(resultSet.getString("hobby"));
			         talentEntity.setPhone(resultSet.getString("phone"));
			         talentEntity.setGender(resultSet.getInt("gender"));
			}else{
				System.out.println("登录失败！");
				return talentEntity;
			}
			
//			//4、关闭连接
//			resultSet.close();
//			preparedStatement.close();
//			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return talentEntity;
	}

	/**
	 * 求职者注册
	 * 
	 * @param talent
	 */
	public int register(TalentEntity talent) {
        int row = 0;
		try {
			//1、加载驱动
			//Class.forName("com.mysql.jdbc.Driver");
			//2、连接数据库
			//Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hiring", "root", "root");
			Connection connection = DBUtil.getConnection();
			//3、操作数据库
			String sql="insert into talent values(null,?,?,?,?,?,?,?,?)";
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, talent.getCode());
			preparedStatement.setString(2, talent.getPassword());
			preparedStatement.setString(3, talent.getName());
			preparedStatement.setString(4, talent.getPhone());
			preparedStatement.setString(5, talent.getEmail());
			preparedStatement.setInt(6, talent.getAge());
			preparedStatement.setInt(7, talent.getGender());
			preparedStatement.setString(8, talent.getHobby());
			row = preparedStatement.executeUpdate();
			//4、关闭连接
			preparedStatement.close();
			connection.close();
		    } catch (SQLException e) {
			  e.printStackTrace();
		    }
		return row;
	}
	
	/**
	 * 修改密码
	 */
	public  int updatePassword(int id,String newpwd) {
	int row=0;
	
	try {
	
	Connection connection = DBUtil.getConnection();
	String  sql="UPDATE  talent  SET  password=?  WHERE  id=?";
	PreparedStatement preparedStatement = connection.prepareStatement(sql);
	preparedStatement.setString(1,newpwd);
	preparedStatement.setInt(2,id);
	row = preparedStatement.executeUpdate();
	DBUtil.close(preparedStatement,connection);
	} catch (Exception e) {
		
	}
	return  row;
		 
	}
	/**
	 * 修改求职者信息
	 * @param talent
	 * @return
	 */
	public int changeInfo(TalentEntity talent) {
		 int row = 0;
			try {
				Connection connection = DBUtil.getConnection();
				String sql="UPDATE talent SET name=?,phone=?,email=?,age=?,gender=?,hobby=? WHERE id = ?";
				PreparedStatement preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setString(1, talent.getName());
				preparedStatement.setString(2, talent.getPhone());
				preparedStatement.setString(3, talent.getEmail());
				preparedStatement.setInt(4, talent.getAge());
				preparedStatement.setInt(5, talent.getGender());
				preparedStatement.setString(6, talent.getHobby());
				preparedStatement.setInt(7, talent.getId());
				row = preparedStatement.executeUpdate();
				//4、关闭连接
				preparedStatement.close();
				connection.close();
			    } catch (SQLException e) {
				  e.printStackTrace();
			    }
			return row;
	}
	
	
	
	/**
	 * 注册时查询账号是否存在
	 * @param valueCode
	 * @return
	 */
	public int selectCode(String valueCode) {
		int row =0;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		 try {
			 	String sql = "SELECT * FROM talent t WHERE t.code=?";
				prepareStatement = connection.prepareStatement(sql);
				prepareStatement.setString(1,valueCode);
				resultSet = prepareStatement.executeQuery();
				if(resultSet.next()){
					row = 1;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return row;
	}
	
	

}
